import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router';
import { useAuthStore } from '@/stores/auth';
import { useConfigStore } from '@/stores/config';

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    redirect: '/dashboard',
    component: () => import('@/layouts/default-layout/DefaultLayout.vue'),
    meta: {
      middleware: 'auth',
    },
    children: [
      {
        path: '/dashboard',
        name: 'dashboard',
        component: () => import('@/views/Dashboard.vue'),
        meta: {
          pageTitle: '主工作台',
          breadcrumbs: ['主工作台'],
        },
      },
    ],
  },
  {
    path: '/',
    redirect: '/analysis',
    component: () => import('@/layouts/default-layout/DefaultLayout.vue'),
    meta: {
      middleware: 'auth',
    },
    children: [
      {
        path: '/repo-analysis',
        name: 'repo-analysis',
        component: () => import('@/views/analysis/RepoAnalysis.vue'),
        meta: {
          pageTitle: '仓库分析',
          breadcrumbs: ['分析', '仓库分析'],
        },
      },
      {
        path: '/repo-author-analysis',
        name: 'repo-author-analysis',
        component: () => import('@/views/analysis/RepoAuthorAnalysis.vue'),
        meta: {
          pageTitle: '仓库贡献分析',
          breadcrumbs: ['分析', '仓库贡献分析'],
        },
      },
      {
        path: '/proj-analysis',
        name: 'proj-analysis',
        component: () => import('@/views/analysis/ProjAnalysis.vue'),
        meta: {
          pageTitle: '项目分析',
          breadcrumbs: ['分析', '项目分析'],
        },
      },
      {
        path: '/proj-author-analysis',
        name: 'proj-author-analysis',
        component: () => import('@/views/analysis/ProjAuthorAnalysis.vue'),
        meta: {
          pageTitle: '项目贡献分析',
          breadcrumbs: ['分析', '项目贡献分析'],
        },
      },
      {
        path: '/engineer-analysis',
        name: 'engineer-analysis',
        component: () => import('@/views/analysis/EngineerAnalysis.vue'),
        meta: {
          pageTitle: '工程师分析',
          breadcrumbs: ['分析', '工程师分析'],
        },
      },
      {
        path: '/commitlog-period-analysis',
        name: 'commitlog-period-analysis',
        component: () => import('@/views/analysis/CommitLogPeridoAnalysis.vue'),
        meta: {
          pageTitle: '日志时段分析',
          breadcrumbs: ['分析', '日志', '日志时段分析'],
        },
      },
      {
        path: '/commitlog-amount-analysis',
        name: 'commitlog-amount-analysis',
        component: () => import('@/views/analysis/CommitLogAmountAnalysis.vue'),
        meta: {
          pageTitle: '日志数量分析',
          breadcrumbs: ['分析', '日志', '日志数量分析'],
        },
      },
    ],
  },
  {
    path: '/',
    redirect: '/biz',
    component: () => import('@/layouts/default-layout/DefaultLayout.vue'),
    meta: {
      middleware: 'auth',
    },
    children: [
      {
        path: '/projects',
        name: 'projects',
        component: () => import('@/views/biz/ProjectRepos.vue'),
        meta: {
          pageTitle: '项目',
          breadcrumbs: ['业务', '项目'],
        },
      },
      {
        path: '/dept-engineer',
        name: 'dept-engineer',
        component: () => import('@/views/biz/DeptEngineer.vue'),
        meta: {
          pageTitle: '部门工程师',
          breadcrumbs: ['业务', '部门工程师'],
        },
      },
      {
        path: '/commitlog-monitor',
        name: 'commitlog-monitor',
        component: () => import('@/views/biz/CommitLogMonitor.vue'),
        meta: {
          pageTitle: '日志监控',
          breadcrumbs: ['业务', '日志监控'],
        },
      },
      {
        path: '/repo-monitor',
        name: 'repo-monitor',
        component: () => import('@/views/biz/RepoMonitor.vue'),
        meta: {
          pageTitle: '仓库监控',
          breadcrumbs: ['业务', '仓库监控'],
        },
      },
    ],
  },
  {
    path: '/',
    component: () => import('@/layouts/AuthLayout.vue'),
    children: [
      {
        path: '/sign-in',
        name: 'sign-in',
        component: () => import('@/views/crafted/authentication/basic-flow/SignIn.vue'),
        meta: {
          pageTitle: '登录',
        },
      },
      {
        path: '/sign-up',
        name: 'sign-up',
        component: () => import('@/views/crafted/authentication/basic-flow/SignUp.vue'),
        meta: {
          pageTitle: '注册',
        },
      },
      {
        path: '/password-reset',
        name: 'password-reset',
        component: () => import('@/views/crafted/authentication/basic-flow/PasswordReset.vue'),
        meta: {
          pageTitle: '密码重置',
        },
      },
    ],
  },
  {
    path: '/',
    component: () => import('@/layouts/SystemLayout.vue'),
    children: [
      {
        // the 404 route, when none of the above matches
        path: '/404',
        name: '404',
        component: () => import('@/views/crafted/authentication/Error404.vue'),
        meta: {
          pageTitle: '错误 404',
        },
      },
      {
        path: '/500',
        name: '500',
        component: () => import('@/views/crafted/authentication/Error500.vue'),
        meta: {
          pageTitle: '错误 500',
        },
      },
    ],
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: '/404',
  },
];

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
  scrollBehavior(to) {
    // If the route has a hash, scroll to the section with the specified ID; otherwise, scroll to the top of the page.
    if (to.hash) {
      return {
        el: to.hash,
        top: 80,
        behavior: 'smooth',
      };
    } else {
      return {
        top: 0,
        left: 0,
        behavior: 'smooth',
      };
    }
  },
});

router.beforeEach((to, from, next) => {
  const authStore = useAuthStore();
  const configStore = useConfigStore();

  // current page view title
  document.title = `${to.meta.pageTitle} - ${import.meta.env.VITE_APP_NAME}`;

  // reset config to initial state
  configStore.resetLayoutConfig();

  // verify auth token before each page change
  authStore.verifyAuth();

  // before page access check if page requires authentication
  if (to.meta.middleware == 'auth') {
    if (authStore.isAuthenticated) {
      next();
    } else {
      next({ name: 'sign-in' });
    }
  } else {
    next();
  }
});

export default router;
